/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbBusinessPattern;
import storage.util.StorageManager;

/**
 * 
 * @author fenyo rome
 *
 */
public class BusinessPatternHelper {

	/**
	 * 将BusinessPattern保存到LaUDMS数据库中
	 * 
	 * @param businessPattern
	 *            要存储的模型对象
	 */
	public void saveBusinessPattern(DbBusinessPattern businessPattern) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_businessPattern"));
		sm.setRowKey(businessPattern.getRowKey());
		sm.put(DbBusinessPattern.CL_NAME, businessPattern.getName());
		sm.put(DbBusinessPattern.CL_UPDATETIME, businessPattern.getUpdateTime());
		sm.put(DbBusinessPattern.CL_TIMESTAMP, businessPattern.getTimeStamp());
		sm.put(DbBusinessPattern.CL_MAINCASEID, businessPattern.getMainCaseID());
		sm.put(DbBusinessPattern.CL_PRODUCT, businessPattern.getProduct());
		ArrayList<String> activityList = businessPattern.getActivityList();
		for (int i = 0; i < activityList.size(); i++) {
			sm.put(DbBusinessPattern.CL_ACTIVITY + "_" + activityList.get(i),
					activityList.get(i));
		}
		ArrayList<String> caseIDList = businessPattern.getCaseIDList();
		for (int i = 0; i < caseIDList.size(); i++) {
			sm.put(DbBusinessPattern.CL_CASEID + "_" + caseIDList.get(i),
					caseIDList.get(i));
		}
		ArrayList<String> attributeList = businessPattern.getAttributeList();
		for (int i = 0; i < attributeList.size(); i++) {
			sm.put(DbBusinessPattern.CL_ATTRIBUTE + "_" + attributeList.get(i),
					attributeList.get(i));
		}
		sm.put(DbBusinessPattern.CL_CLASSIFYTAG,
				businessPattern.getClassifytag());

	}

	/**
	 * 将一系列logHead保存到LaUDMS数据库中
	 * 
	 * @param businessPatterns
	 *            要存储的模型对象列表
	 */
	public void saveBusinessPatterns(List<DbBusinessPattern> businessPatterns) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_businessPattern"));
		// sm.open();
		for (int j = 0; j < businessPatterns.size(); j++) {
			DbBusinessPattern businessPattern = businessPatterns.get(j);
			sm.setRowKey(businessPattern.getRowKey());
			sm.put(DbBusinessPattern.CL_NAME, businessPattern.getName());
			sm.put(DbBusinessPattern.CL_UPDATETIME,
					businessPattern.getUpdateTime());
			sm.put(DbBusinessPattern.CL_TIMESTAMP,
					businessPattern.getTimeStamp());
			sm.put(DbBusinessPattern.CL_MAINCASEID,
					businessPattern.getMainCaseID());
			sm.put(DbBusinessPattern.CL_PRODUCT, businessPattern.getProduct());
			ArrayList<String> activityList = businessPattern.getActivityList();
			for (int i = 0; i < activityList.size(); i++) {
				sm.put(DbBusinessPattern.CL_ACTIVITY + "_"
						+ activityList.get(i), activityList.get(i));
			}
			ArrayList<String> caseIDList = businessPattern.getCaseIDList();
			for (int i = 0; i < caseIDList.size(); i++) {
				sm.put(DbBusinessPattern.CL_CASEID + "_" + caseIDList.get(i),
						caseIDList.get(i));
			}
			ArrayList<String> attributeList = businessPattern
					.getAttributeList();
			for (int i = 0; i < attributeList.size(); i++) {
				sm.put(DbBusinessPattern.CL_ATTRIBUTE + "_"
						+ attributeList.get(i), attributeList.get(i));
			}
			sm.put(DbBusinessPattern.CL_CLASSIFYTAG,
					businessPattern.getClassifytag());
			// sm.close();
		}
	}
	/**
	 * 
	 * @param columnName
	 * columnName
	 * @param value
	 * value	
	 * @return
	 */
	public List<DbBusinessPattern> getBusinessPatterns(final String columnName,
			final String value) {
		List<DbBusinessPattern> result = new LinkedList<DbBusinessPattern>();
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_businessPattern"));
		// sm.open();
		List<KeySlice> crs = sm.get(columnName, value);
		try {
			for (KeySlice ks : crs) {
				String rowKey = new String(ks.getKey());
				String name = null, product = null, mainCaseID = null, updateTime = null, timeStamp = null, classifytag = null;
				ArrayList<String> activityList = new ArrayList();
				ArrayList<String> caseIDList = new ArrayList();
				ArrayList<String> attributeList = new ArrayList();

				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbBusinessPattern.CL_NAME)) {
						name = columnsValue;
					} else if (columnsName.equals(DbBusinessPattern.CL_PRODUCT)) {
						product = columnsValue;
					} else if (columnsName
							.equals(DbBusinessPattern.CL_MAINCASEID)) {
						mainCaseID = columnsValue;
					} else if (columnsName
							.equals(DbBusinessPattern.CL_TIMESTAMP)) {
						timeStamp = columnsValue;
					} else if (columnsName
							.equals(DbBusinessPattern.CL_UPDATETIME)) {
						updateTime = columnsValue;
					} else if (columnsName
							.startsWith(DbBusinessPattern.CL_ACTIVITY)) {
						activityList.add(columnsValue);
					} else if (columnsName
							.startsWith(DbBusinessPattern.CL_CASEID)) {
						caseIDList.add(columnsValue);
					} else if (columnsName
							.startsWith(DbBusinessPattern.CL_ATTRIBUTE)) {
						attributeList.add(columnsValue);
					} else if (columnsName
							.equals(DbBusinessPattern.CL_CLASSIFYTAG)) {
						classifytag = columnsValue;
					}
				}
				DbBusinessPattern pBusinessPattern = new DbBusinessPattern(
						rowKey, name, product, mainCaseID, timeStamp,
						updateTime, caseIDList, activityList, attributeList,
						classifytag);
				result.add(pBusinessPattern);
			}
		} catch (Exception ex) {
			Logger.getLogger(DbBusinessPattern.class.getName()).log(
					Level.SEVERE, null, ex);
		}
		return result;
	}
/**
 * 
 * @return ArrayList<String> 
 */
	public ArrayList<String> getAllBusinessPatternName() {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_businessPattern"));
		// sm.open();
		List<KeySlice> crs = sm.getAll();
		ArrayList<String> businessPatternNumList = new ArrayList<String>();
		try {
			for (KeySlice ks : crs) {
				String businessPatternName = null;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbBusinessPattern.CL_NAME)) {
						businessPatternName = columnsValue;
					}
				}
				if (!businessPatternNumList.contains(businessPatternName)) {
					businessPatternNumList.add(businessPatternName);
				}
			}
		} catch (Exception ex) {
			Logger.getLogger(DbBusinessPattern.class.getName()).log(
					Level.SEVERE, null, ex);
		} 
		return businessPatternNumList;
	}
	/**
	 * 
	 * @param rowKey
	 * rowKey
	 */
	public void deleteBusinessPattern(final String rowKey) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_businessPattern"));
		// sm.open();
		sm.delete(rowKey);
		// sm.close();
	}

	/**
	 * 为产品添加日志体字段
	 * 
	 * @param businessPattern
	 *            产品
	 * @param activity
	 *            日志体字段
	 */
	public void addActivity(DbBusinessPattern businessPattern, String activity) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_businessPattern"));
		// sm.open();
		sm.setRowKey(businessPattern.getRowKey());
		sm.put(DbBusinessPattern.CL_ACTIVITY + "_" + activity, activity);
		// sm.close();

	}
	/**
	 * 
	 * @param businessPattern
	 * BusinessPattern
	 * @param caseID
	 * caseID
	 */
	public void addCaseID(DbBusinessPattern businessPattern, String caseID) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_businessPattern"));
		// sm.open();
		sm.setRowKey(businessPattern.getRowKey());
		sm.put(DbBusinessPattern.CL_CASEID + "_" + caseID, caseID);
		// sm.close();

	}


}
